/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_UTIL_SIMPLE_OCEAN_LAYER
#define OSGEARTH_UTIL_SIMPLE_OCEAN_LAYER 1

#include <osgEarth/Common>
#include <osgEarth/ImageLayer>
#include <osgEarth/LayerReference>
#include <osgEarth/URI>
#include <osgEarth/TerrainResources>
#include <osgEarth/Color>

namespace osgEarth
{
    /**
     * A Rex map layer that renders a simple ocean surface.
     * This layer requires that the map include bathymetric data (ocean floor).
     */
    class OSGEARTH_EXPORT SimpleOceanLayer : public VisibleLayer
    {
    public: // serialization
        class OSGEARTH_EXPORT Options : public VisibleLayer::Options {
        public:
            META_LayerOptions(osgEarth, Options, VisibleLayer::Options);
            OE_OPTION(Color, color);
            OE_OPTION(float, maxAltitude);
            OE_OPTION_LAYER(ImageLayer, maskLayer);
            OE_OPTION(bool, useBathymetry);
            OE_OPTION(URI, textureURI);
            OE_OPTION(unsigned, textureLOD);
            OE_OPTION(float, seaLevel);
            OE_OPTION_REFPTR(osg::Image, surfaceImage);
            virtual Config getConfig() const;
        private:
            void fromConfig(const Config& conf);
        };

    public:
        META_Layer(osgEarth, SimpleOceanLayer, Options, VisibleLayer, ocean);

    public:

        //! Ocean surface color (including transparency in the alpha channel)
        void setColor(const Color& color);
        const Color& getColor() const;

        //! Maximum altitude at which the ocean layer is visible
        void setMaxAltitude(const float& altitude_m);
        const float& getMaxAltitude() const;

        //! Sets a masking layer, or pass NULL to clear the masking layer
        void setMaskLayer(ImageLayer* layer);
        ImageLayer* getMaskLayer() const;

        //! Sea level (meters above the ellipsoid)
        void setSeaLevel(const float& seaLevel);
        const float& getSeaLevel() const;

        //! Whether to sample the terrain bathymetry and only draw the ocean
        //! where the elevation is negative (default = true)
        void setUseBathymetry(const bool& value);
        const bool& getUseBathymetry() const;

        //! Surface texture to apply. Call this before adding to the map or 
        //! opening the layer.
        void setSurfaceImage(osg::Image* value);

        //! LOD at which to apply surface texture 1:1 (default = 13)
        void setSurfaceTextureLOD(const unsigned& value);
        const unsigned& getSurfaceTextureLOD() const;

    public: // Layer

        /** callback that ensures proper culling */
        void modifyTileBoundingBox(const TileKey& key, osg::BoundingBox& box) const override;

    protected: // Layer

        virtual void init() override;

        virtual Status openImplementation() override;

        virtual void addedToMap(const class Map*) override;

        virtual void removedFromMap(const class Map*) override;

        virtual void prepareForRendering(TerrainEngine* engine) override;

        virtual Config getConfig() const override;

    protected:

        virtual ~SimpleOceanLayer() { }

    private:

        TextureImageUnitReservation _texReservation;

        void updateMaskLayer();
    };
    
}

#endif // OSGEARTH_UTIL_SIMPLE_OCEAN_LAYER
